//
//  PPNicknameSettingViewController.m
//  pickpic
//
//  Created by Jinxiao on 9/16/16.
//  Copyright © 2016 MicroDream. All rights reserved.
//

#import "PPNicknameSettingViewController.h"

#import "PPNetworkService.h"

@interface PPNicknameSettingViewController ()<UITextViewDelegate>
//<UITextFieldDelegate>
//@property (strong, nonatomic) IBOutlet UITextField *textField;

@property (nonatomic,strong) UITextView *textView;
@end

@implementation PPNicknameSettingViewController
- (UITextView *)textView{

    if (!_textView) {
        _textView = [[UITextView alloc] initWithFrame:self.view.bounds];
        _textView.textContainerInset = UIEdgeInsetsMake(15, 12, 15, 14);
        _textView.delegate = self;
        _textView.font = PPFONTSIZE(16);
        _textView.returnKeyType = UIReturnKeyDone;
    }
    return _textView;
}
- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"昵称";
//    self.tableView.bounces = NO;
    self.view.backgroundColor = WHITE;
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem barButtonItemWithTitle:@"个人资料" image:@"nav_back" target:self action:@selector(popViewPreviousController)];
    
    
    [self.view addSubview:self.textView];
    
    PPAccountProfiles *profiles = [PPAccountManager sharedInstance].account.profiles;
//    _textField.text = profiles.name;

    
    self.textView.text = profiles.name;
    
    __weak typeof(self) weakSelf = self;
    
//    [self setNavigationLeftTitle:@"取消" titleColor:[UIColor colorWithRGBHex:0xf2f2f2] action:^{
//        [weakSelf.navigationController popViewControllerAnimated:YES];
//    }];
    if (IS_IOS_8) {
        [self.navigationItem.rightBarButtonItem setTitleTextAttributes:@{NSFontAttributeName:PFFontMedium(16)} forState:UIControlStateNormal];
    }else{
        [self.navigationItem.rightBarButtonItem setTitleTextAttributes:@{NSFontAttributeName:PPFONT(PFBold, 16)} forState:UIControlStateNormal];
    }
    self.navigationItem.leftBarButtonItem.tintColor = BLACK;
    
    [self setNavigationRightTitle:@"保存" titleColor:[UIColor colorWithRGBHex:0xffdd17] action:^{
        [weakSelf saveItemDidPressed];
    }];
    if (IS_IOS_8) {
        [self.navigationItem.rightBarButtonItem setTitleTextAttributes:@{NSFontAttributeName:PFFontMedium(16)} forState:UIControlStateNormal];
    }else{
        [self.navigationItem.rightBarButtonItem setTitleTextAttributes:@{NSFontAttributeName:PPFONT(PFBold, 16)} forState:UIControlStateNormal];
    }
    self.navigationItem.rightBarButtonItem.tintColor = COLORA(0, 118, 255);
    
 
}

- (void)saveItemDidPressed {
    NSString *nickname = self.textView.text;
    NSRange range = [nickname rangeOfString:@" "];
    if (range.location != NSNotFound) {
        [self.navigationController.view toastMessage:@"昵称不能包含空格哦" duration:1.f];
        return;
    }
    NSInteger count = 0; //汉字
    NSInteger count1 =0; //英文
    for (NSInteger i = 0; i < [nickname length]; i++) {
        unichar c = [nickname characterAtIndex:i];
        if (c >=0x4E00 && c <=0x9FA5)
        {
            count ++;
        }
        else
        {
            count1 ++;

        }
    }
    if (count1 == 0 && count == 0)
    {
        [self.navigationController.view toastMessage:@"昵称至少要包含一个字母或汉字哦" duration:1.f];
        return;
    }
//    if (count > 16)
//    {
//        [self.navigationController.view toastMessage:@"昵称不能超过16个汉字哦" duration:1.f];
//        return;
//    }
//    if (count1 > 32)
//    {
//        [self.navigationController.view toastMessage:@"昵称不能超过32个字母哦" duration:1.f];
//        return;
//    }
    if (count1 + count*2 > 32)
    {
        [self.navigationController.view toastMessage:@"昵称长度不能超过16哦" duration:1.f];
        return;
    }
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];

    PPAccountProfiles *profiles = [PPAccountManager sharedInstance].account.profiles;
    PPUserID userID = profiles.userID;

    __weak typeof(self) weakSelf = self;
    [PPNetworkService updateProfileWithName:nickname gender:nil country:nil state:nil city:nil birthday:nil signature:nil type:1 callback:^(id data, NSError *error) {
        __strong typeof(weakSelf) strongSelf = weakSelf;
        if(strongSelf == nil)
        {
            return;
        }

        [hud hide:YES];

        if(error == nil) {
            PPAccountProfiles *profiles = [PPAccountManager sharedInstance].account.profiles;

            if([profiles.userID isEqualToString:userID]) {
                profiles.name = nickname;
                [profiles.managedObjectContext saveContext];
            }

            [[NSNotificationCenter defaultCenter] postNotificationName:PPAccountProfilesNicknameDidChangeNotification object:nickname];

            [strongSelf.navigationController.view toastMessage:@"变更成功" duration:1.f completion:^{
                [strongSelf.navigationController popViewControllerAnimated:YES];
            }];
        } else {
            [strongSelf.navigationController.view toastMessage:error.networkPrompt];
        }

    }];
}


-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range
replacementText:(NSString *)text{

    if([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    
    NSString *nickname = [textView.text stringByReplacingCharactersInRange:range withString:text];
    
    UIBarButtonItem *confirmItem = self.navigationItem.rightBarButtonItem;
    if(nickname.length > 0) {
        confirmItem.enabled = YES;
        confirmItem.tintColor = [UIColor colorWithRGBHex:0xffdd17];
    } else {
        confirmItem.enabled = NO;
        confirmItem.tintColor = [UIColor colorWithRGBHex:0xb2b2b2];
    }
    
    return YES;

}
//#pragma mark - UITextFieldViewDelegate
//- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
//
//    if([string isEqualToString:@"\n"]) {
//        [textField resignFirstResponder];
//        return NO;
//    }
//
//    NSString *nickname = [textField.text stringByReplacingCharactersInRange:range withString:string];
//
//    UIBarButtonItem *confirmItem = self.navigationItem.rightBarButtonItem;
//    if(nickname.length > 0) {
//        confirmItem.enabled = YES;
//        confirmItem.tintColor = [UIColor colorWithRGBHex:0xffdd17];
//    } else {
//        confirmItem.enabled = NO;
//        confirmItem.tintColor = [UIColor colorWithRGBHex:0xb2b2b2];
//    }
//
//    return YES;
//}

//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
//{
//    return 15;
//}

@end
